package cn.wolfcode.service.impl;

import cn.wolfcode.domain.Permission;
import cn.wolfcode.mapper.PermissionMapper;
import cn.wolfcode.qo.PageResult;
import cn.wolfcode.qo.QueryObject;
import cn.wolfcode.service.IPermissionService;
import cn.wolfcode.util.RequiredPermission;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Service;

import java.lang.reflect.Method;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Map;

@Service
public class PermissionServiceImpl implements IPermissionService {
    @Autowired
    private PermissionMapper permissionMapper;

    @Override
    public void delete(Long id) {
        permissionMapper.deleteByPrimaryKey(id);
    }

    @Override
    public void save(Permission permission) {
        permissionMapper.insert(permission);
    }

    @Override
    public Permission get(Long id) {
        return permissionMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<Permission> listAll() {
        return permissionMapper.selectAll();
    }

    @Override
    public void update(Permission permission) {
        permissionMapper.updateByPrimaryKey(permission);
    }

    @Override
    public PageResult<Permission> query(QueryObject qo) {
        int count = permissionMapper.selectForCount(qo);
        int totalPage = count % qo.getPageSize() == 0 ? count / qo.getPageSize() : count / qo.getPageSize() + 1;
        if (qo.getCurrentPage() > totalPage) {
            qo.setCurrentPage(totalPage);
        }
        if (count == 0) {
            return new PageResult<>(qo.getCurrentPage(), qo.getPageSize(), Collections.emptyList(), count);
        }
        List<Permission> permissions = permissionMapper.selectForList(qo);
        return new PageResult<>(qo.getCurrentPage(), qo.getPageSize(), permissions, count);

    }

    @Autowired
    private ApplicationContext ctx;

    @Override
    public void reload() {

        //先获取数据库中的权限表达式
        List<String> permissions = permissionMapper.selectAllExpression();
        //1.获取所有的Controller,
        // 利用spring上下文,获取带有Controller 注解的bean
        Map<String, Object> map = ctx.getBeansWithAnnotation(Controller.class);

        Collection<Object> controllers = map.values();
        //2.遍历所有的Controller ,获取所有的方法
        for (Object controller : controllers) {
            Class clazz = controller.getClass();//字节码对象
            Method[] methods = clazz.getDeclaredMethods();//字节码中的所有方法
            //3.遍历每个方法,判断方法上是否有贴自定义注解
            for (Method method : methods) {
                RequiredPermission annotation = method.getAnnotation(RequiredPermission.class);
                //判断注解是否为空
                if (annotation != null) {
                    //不为空执行下边操作
                    //4.获取注解上的权限名称以及权限表达式
                    String name = annotation.name();//权限名称
                    String expression = annotation.expression();//权限表达式
                    if (!permissions.contains(expression)) {//获取的权限表达式不在数据库中
                        //5.封装成权限对象,创建一个新的权限对象,通过setter方法设值
                        Permission permission = new Permission();
                        permission.setName(name);
                        permission.setExpression(expression);
                        //6.将权限对象插入到数据库中
                        permissionMapper.insert(permission);
                    }
                }
            }
        }
    }

    @Override
    public List<String> selectByEmpId(Long id) {

        return permissionMapper.selectByEmpId(id);
    }
}
